﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.SYS.Orders_Goods
{
    public partial class List : ListPage
    {
        BLL.V_Orders_Goods bllList = new BLL.V_Orders_Goods();
        BLL.Orders_Goods bll = new BLL.Orders_Goods();
        //
        BLL.Orders bllOrders = new BLL.Orders();
        Model.Orders modelOrders = new Model.Orders();
        protected int OrdersState = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            Master.show_btn_add = Master.show_btn_bat_del = Master.show_edit = false;
            if (Security.GetCurrentUserType() != EnumHelper.UserType.Store) bat_add2order.Visible = Master.show_checkbox = false;
            
            if (search_radio_actualamount.SelectedValue != "0")
            {
                Master.ListSumColumn.Add(new SumColumn("ActualPriceSum", EnumHelper.SumType.Coin));
                
            }
            else
            {
                th_ActualAmount.Visible = false;
                th_ActualPrice.Visible = false;
                th_ActualPriceSum.Visible = false;
                Master.ArrayControlID_noVisible_cph_td.Add("td_ActualPrice");
                Master.ArrayControlID_noVisible_cph_td.Add("td_ActualPriceSum");
            }

            int OrdersID = DataTypePlus.requestInt( Request.QueryString["OrdersID"]);
            modelOrders = bllOrders.GetModel(OrdersID);
            OrdersState = modelOrders.State;
            Master.BasicSQL = "OrdersID=" + OrdersID;
            Master.orderBy = "IsDelivery desc, SupplierID,Dateandtime desc";
            if (modelOrders.State < (int)EnumHelper.OrdersState.BuyFinish || search_radio_actualamount.SelectedValue == "0")
            {
                th_ActualAmount.Visible = false;
                th_ActualPrice.Visible = false;
                th_ActualPriceSum.Visible = false;
                Master.ArrayControlID_noVisible_cph_td.Add("td_ActualAmount");
                Master.ArrayControlID_noVisible_cph_td.Add("td_ActualPrice");
                Master.ArrayControlID_noVisible_cph_td.Add("td_ActualPriceSum");
            }
        }
        public override void buildSQL_Page_List_search()
        {
            if (search_radio_isdelivery.SelectedValue != "")
                Master.SearchSQL = StringPlus.SQLand(Master.SearchSQL, "IsDelivery=" + search_radio_isdelivery.SelectedValue + "");
            if (search_radio_actualamount.SelectedValue != "")
            {
                if(search_radio_actualamount.SelectedValue=="0")
                    Master.SearchSQL = StringPlus.SQLand(Master.SearchSQL, "ActualAmount=0");
                else
                    Master.SearchSQL = StringPlus.SQLand(Master.SearchSQL, "ActualAmount>0");
            }
                
        }
        public override DataSet get_Page_DataSet(string sql_, string orderby_, int pageFrom_, int pageTo_) { return bllList.GetListByPage(sql_, orderby_, pageFrom_, pageTo_); }
        public override void bat_Page_List(string inStr_, string action_)
        {
            if (action_ == "add2order")
            {
                //当前的订单
                Model.Orders modelOrders_Now = BLL.Orders.GetModelOrders_Now(Security.GetCurrentStore());
                foreach (string id in inStr_.Split(','))
                {
                    int thisID = DataTypePlus.requestInt(id);
                    //点击的历史订单货品
                    Model.Orders_Goods modelOrders_Goods_History = bll.GetModel(thisID);
                    //要对当前订单操作的订单货品
                    Model.Orders_Goods modelOrders_Goods_Now = new Model.Orders_Goods();
                    //当前订单可能存在的本货品记录
                    List<Model.Orders_Goods> listModelOrders_Goods_Now_Maybe = bll.GetModelList("OrdersID=" + modelOrders_Now.id + " and GoodsID=" + modelOrders_Goods_History.GoodsID);
                    
                    if (listModelOrders_Goods_Now_Maybe.Count > 0)
                    {//当前订单存在此货品，更新
                        modelOrders_Goods_Now = listModelOrders_Goods_Now_Maybe[0];
                        modelOrders_Goods_Now.ExpectAmount += modelOrders_Goods_History.ExpectAmount;
                        modelOrders_Goods_Now.WaitSupplierCheckNum += modelOrders_Goods_History.ExpectAmount;
                        bll.Update(modelOrders_Goods_Now);
                        BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Orders_Goods, modelOrders_Goods_Now.id, EnumHelper.OperateType.Update, "再次续订" + modelOrders_Goods_History.ExpectAmount + "个");
                    }
                    else
                    {//当前订单不存在此货品，添加
                        modelOrders_Goods_Now.OrdersID = modelOrders_Now.id;
                        modelOrders_Goods_Now.GoodsID = modelOrders_Goods_History.GoodsID;
                        modelOrders_Goods_Now.ExpectAmount = modelOrders_Goods_History.ExpectAmount;
                        modelOrders_Goods_Now.WaitSupplierCheckNum = modelOrders_Goods_History.ExpectAmount;
                        int newOrdersID=bll.Add(modelOrders_Goods_Now);
                        BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Orders_Goods, newOrdersID, EnumHelper.OperateType.Add, "续订" + modelOrders_Goods_History.ExpectAmount + "个");
                    }
                    

                }
            }
        }
    }
}